// ZonePersist.cpp: implementation of the ZonePersist class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ZonePersist.h"
#include "Zone.h"
#include "DatabaseBase.h"
#include "ZoneTable.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

using namespace PDMS::PDMSDBIF;
using namespace PDMS::PDMSDomain;

BEGIN_REGISTER_MAP( ZonePersist, PersistRegBase )
REGISTER( UE_RUNTIME_CLASS( Zone ), GET_REGISTER( PersistRegBase, VersionOne ) )
END_REGISTER_MAP();

ZonePersist::ZonePersist()
{

}

ZonePersist::~ZonePersist()
{

}

UESoft::ErrorStatus ZonePersist::Retrieve( Persistable*& pObj )
{
	UESoft::ErrorStatus es = ReadPaternity( pObj );
	if ( es != UESoft::eOk )
	{
		return es;
	}

	if ( NULL == pObj )
	{
		return UESoft::eNullEntityPointer;
	}
	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Zone ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}
	Zone* pZone = static_cast<Zone*>( pObj );

	StdString strCondition;
	strCondition.Format( _T("ID = %ld"), pObj->Id().ObjectID() );
	
	DatabaseBase* pBase = NULL;
	pBase = GetDatabase();
	ASSERT( pBase != NULL );
	DatabaseElement* pDbE = NULL;
	pDbE = pBase->GetDatabaseElement( _T("ZoneData") );
	ASSERT( pDbE != NULL );
	
	ZoneTable* pZT = static_cast<ZoneTable*>( pDbE );
	es = pZT->Open( strCondition );
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pZone->OwnerId( UeObjectId( pZone->Id().EnviromentID(), pZT->GetParentID() ) );
	pZone->Description( pZT->GetDescription() );
	pZone->SetPurpose( pZT->GetPurpose() );
	pZone->SetFunction( pZT->GetFunction() );
	pZone->SetNumber( pZT->GetNumber() );
	pZone->SetArea( pZT->GetArea() );
	pZone->SetDUnion( pZT->GetDUnion() );
	pZone->SetModule( pZT->GetModule() );
	pZone->SetRlstored( pZT->GetRlstored() );

	return UESoft::eOk;
}

UESoft::ErrorStatus ZonePersist::Create( Persistable* pObj )
{
	UESoft::ErrorStatus es = CreatePaternity( pObj );
	if ( es != UESoft::eOk )
	{
		return es;
	}

	if ( NULL == pObj )
	{
		return UESoft::eNullEntityPointer;
	}
	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Zone ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}
	Zone* pZone = static_cast<Zone*>( pObj );
	
	DatabaseBase* pBase = NULL;
	pBase = GetDatabase();
	ASSERT( pBase != NULL );
	DatabaseElement* pDbE = NULL;
	pDbE = pBase->GetDatabaseElement( _T("ZoneData") );
	ASSERT( pDbE != NULL );
	
	ZoneTable* pZT = static_cast<ZoneTable*>( pDbE );
	es = pZT->Open();
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pZT->NewRecord();
	pZT->SetID( pZone->Id().ObjectID() );
	pZT->SetParentID( pZone->OwnerId().ObjectID() );
	pZT->SetDescription( pZone->Description() );
	pZT->SetPurpose( pZone->GetPurpose() );
	pZT->SetFunction( pZone->GetFunction() );
	pZT->SetNumber( pZone->GetNumber() );
	pZT->SetArea( pZone->GetArea() );
	pZT->SetDUnion( pZone->GetDUnion() );
	pZT->SetModule( pZone->GetModule() );
	pZT->SetRlstored( pZone->GetRlstored() );

	return UESoft::eOk;
}

UESoft::ErrorStatus ZonePersist::Update( Persistable* pObj )
{
	if ( NULL == pObj )
	{
		return UESoft::eNullEntityPointer;
	}
	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Zone ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}
	Zone* pZone = static_cast<Zone*>( pObj );
	
	DatabaseBase* pBase = NULL;
	pBase = GetDatabase();
	ASSERT( pBase != NULL );
	DatabaseElement* pDbE = NULL;
	pDbE = pBase->GetDatabaseElement( _T("ZoneData") );
	ASSERT( pDbE != NULL );
	
	ZoneTable* pZT = static_cast<ZoneTable*>( pDbE );
	UESoft::ErrorStatus es = pZT->Open();
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pZT->UpdateRecord( pZone->Id().ObjectID() );
	pZT->SetParentID( pZone->OwnerId().ObjectID() );
	pZT->SetDescription( pZone->Description() );
	pZT->SetPurpose( pZone->GetPurpose() );
	pZT->SetFunction( pZone->GetFunction() );
	pZT->SetNumber( pZone->GetNumber() );
	pZT->SetArea( pZone->GetArea() );
	pZT->SetDUnion( pZone->GetDUnion() );
	pZT->SetModule( pZone->GetModule() );
	pZT->SetRlstored( pZone->GetRlstored() );

	return UESoft::eOk;
}